<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" :close-on-press-escape="mode != 'show' ? false : true"
             destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="120px" label-position="left">
			<el-form-item label="存储方式" prop="engine">
				<el-radio-group v-model="form.engine">
					<el-radio-button label="qiniu">七牛云</el-radio-button>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="存储空间名称" prop="bucket">
				<el-input v-model="form.bucket" clearable placeholder="请输入存储空间名称(Bucket)"></el-input>
			</el-form-item>
			<el-form-item label="ACCESS_KEY" prop="access_key">
				<el-input v-model="form.access_key" clearable placeholder="请输入ACCESS_KEY(AK)"></el-input>
			</el-form-item>
			<el-form-item label="SECRET_KEY" prop="secret_key">
				<el-input v-model="form.secret_key" clearable placeholder="请输入SECRET_KEY(SK)"></el-input>
			</el-form-item>
			<el-form-item label="空间域名" prop="domain">
				<el-input v-model="form.domain" clearable placeholder="请输入空间域名(Domain)"></el-input>
				<div class="el-form-item-msg">请补全http://或https://，例如https://static.cloud.com</div>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-switch v-model="form.status" :active-value="1" :inactive-value="0"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "edit",
				titleMap: {
					edit: '设置存储方式'
				},
				form: {
					engine: '',
					bucket: '',
					access_key: '',
					secret_key: '',
					domain: '',
					status: 1
				},
				rules: {
					engine:[
						{required: true, message: '请选择存储方式'}
					],
					bucket:[
						{required: true, message: '请输入存储空间名称'}
					],
					access_key:[
						{required: true, message: '请输入ACCESS_KEY'}
					],
					secret_key:[
						{required: true, message: '请输入SECRET_KEY'}
					],
					domain:[
						{required: true, message: '请输入空间域名'}
					],
				},
				visible: false,
				isSaveing: false
			}
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.setting.storage.setup.post(this.form);
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.engine = data.engine
				this.detail(data.engine)
			},
			async detail(engine){
				var res = await this.$API.setting.storage.detail.get({engine:engine});
				console.log(res,'res')
				if(res.code == 1){
					this.form.status = res.data.status
					this.form.bucket = res.data.bucket
					this.form.access_key = res.data.access_key
					this.form.secret_key = res.data.secret_key
					this.form.domain = res.data.domain
				}
			},
		}
	}
</script>

<style>
</style>
